package com.talking.view;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.bumptech.glide.Glide;
import com.github.dhaval2404.imagepicker.ImagePicker;
import com.talking.App;
import com.talking.R;
import com.talking.bean.GroupBean;
import com.talking.databinding.ActivityCreateGroupBinding;
import com.talking.presenter.GroupPresenter;
import com.talking.utils.Utils;

import org.jetbrains.annotations.NotNull;
import org.json.JSONObject;

import java.io.File;
import java.io.IOException;
import java.util.Objects;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class CreateGroupActivity extends AppCompatActivity {
    private ActivityCreateGroupBinding binding;
    private ActivityResultLauncher<Intent> launcher;
    private String iconDir="";

    @Override
    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        binding=ActivityCreateGroupBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        Objects.requireNonNull(getSupportActionBar()).setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);
        binding.createGroupIcon.setOnClickListener(v -> {
            ImagePicker.with(this).cropSquare().compress(1024).maxResultSize(1080, 1080).createIntent(intent -> {
                launcher.launch(intent);
                return null;
            });
        });
        launcher=registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), result -> {
            int code=result.getResultCode();
            if (code == RESULT_OK) {
                Uri uri = result.getData().getData();
                iconDir = new File(uri.getPath()).getAbsolutePath();
                Glide.with(this).load(uri).apply(Utils.getGlideOption()).placeholder(R.drawable.loading).placeholder(R.drawable.loading).error(R.drawable.loaderr).into(binding.createGroupIcon);
            } else if (code == ImagePicker.RESULT_ERROR) {
                Utils.toast(ImagePicker.getError(result.getData()));
            }
        });

    }
    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (android.R.id.home==item.getItemId()){ finish(); }
        if (item.getItemId()== R.id.menu_group_create){
            if (iconDir.equals("")){
                Utils.toast( "请选择头像");
            }else if (binding.createGroupName.getText().toString().equals("")){
                Utils.toast( "请输入群聊名称");
            }else {
                GroupPresenter.create(binding.createGroupName.getText().toString(), App.sp.getString("account", ""), new File(iconDir), new Callback() {
                    @Override
                    public void onFailure(@NotNull Call call, @NotNull IOException e) {
                        e.printStackTrace();
                    }
                    @Override
                    public void onResponse(@NotNull Call call, @NotNull Response response) {
                        runOnUiThread(() -> {
                            try {
                                JSONObject jo = new JSONObject(response.body().string());
                                App.groupBean=new GroupBean(jo.getString("groupName"),jo.getString("groupIcon"),jo.getString("id"));
                                startActivity(new Intent(CreateGroupActivity.this,ChatGroupActivity.class));
                                finish();
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        });
                    }
                });
            }
        }
        return super.onOptionsItemSelected(item);
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_create_group, menu);
        return true;
    }
}
